/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: EquipableItem.java 44 2008-05-14 18:22:13Z jens464 $
*/
package albareth.core.entity;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import albareth.core.entity.attribute.ItemAttribute;
import albareth.frontend.Tile;

public abstract class EquipableItem extends Item implements Equipable, Cloneable
{
    private int _equipSlot = PlayerCharacter.SLOT_NONE;
    private int _strReq = 0;
    private int _dexReq = 0;
    
    private LinkedList _itemAttributes = null;

    
    public EquipableItem(String name, Tile tile, int equipSlot)
    {
        super(name, tile);
        _equipSlot = equipSlot;
    }

    public Object clone() throws CloneNotSupportedException
    {
        EquipableItem clone = (EquipableItem)super.clone();
        if (_itemAttributes != null) {
            clone._itemAttributes = (LinkedList)_itemAttributes.clone();
        }
        return clone; 
    }


    public boolean isEquipable()
    {
        return _equipSlot != PlayerCharacter.SLOT_NONE;
    }
    
    public void setEquipSlot(int equipSlot)
    {
        _equipSlot = equipSlot;
    }
    
    public int getEquipSlot()
    {
        return _equipSlot;
    }

    /**
     * @return true if can be equipped, false otherwise.
     */
    public boolean equip(Mobile src)
    {
        if (!isEquipable()) {
            return false;
        }
        
        //check reqs if player.
        if (src instanceof PlayerCharacter)
        {
            if (getStrReq() > ((PlayerCharacter)src).getStr()) {
                src.infoMsg("STR " + getStrReq() + " required to equip this item.");
                return false;
            }

            if (getDexReq() > ((PlayerCharacter)src).getDex()) {
                src.infoMsg("DEX " + getDexReq() + " required to equip this item.");
                return false;
            }
        }
        
        if (!onEquip(src)) {
            return false;
        }
        
        applyAttributes(src);
        
        return true;
    }


    /**
     * @return true if can be unequipped, false otherwise.
     */
    public boolean unequip(Mobile src)
    {
        if (!isEquipable()) {
            return false;
        }
        
        if (!onUnequip(src)) {
            return false;
        }
        
        unapplyAttributes(src);

        return true;
    }

    public void addAttribute(ItemAttribute attr)
    {
        if (_itemAttributes == null) {
            _itemAttributes = new LinkedList();
        }
        _itemAttributes.add(attr);
    }
    
    private void applyAttributes(Mobile target)
    {
        if (_itemAttributes == null) {
            return;
        }
        for (Iterator it = _itemAttributes.iterator(); it.hasNext(); )
        {
            ItemAttribute attr = (ItemAttribute)it.next();
            attr.apply(target);
        }
    }
    
    private void unapplyAttributes(Mobile target)
    {
        if (_itemAttributes == null) {
            return;
        }
        for (Iterator it = _itemAttributes.iterator(); it.hasNext(); )
        {
            ItemAttribute attr = (ItemAttribute)it.next();
            attr.unapply(target);
        }

    }

    protected boolean onEquip(Mobile src)
    {
        return true;
    }

    protected boolean onUnequip(Mobile src)
    {
        return true;
    }

    public int getStrReq()
    {
        return _strReq;
    }

    public void setStrReq(int strReq)
    {
        _strReq = strReq;
    }

    public int getDexReq()
    {
        return _dexReq;
    }

    public void setDexReq(int dexReq)
    {
        _dexReq = dexReq;
    }

    public List getItemAttributes()
    {
        return _itemAttributes;
    }




}
